package com.jy.modules.cims.component.image;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.data.common.dao.TCMmQueueDao;
import com.jy.modules.cims.data.common.dto.TCMmQueueDTO;
import com.jy.modules.cims.data.dao.CreditDetailDao;
import com.jy.modules.cims.data.dao.ImgurlDao;
import com.jy.modules.cims.data.dao.LoanDao;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.ImgurlDto;
import com.jy.modules.cims.data.dto.LoanDetailDto;

/**
 * @description TransferImagesDao
 * @author shengzhoutao
 * @date 2015-09-29
 */
@Component
public class TransferImagesDao {
	private static int DEFAULT_PAGE_SIZE = 6;
	
	@Autowired
	private ImgurlDao imgurlDao;

	@Autowired
	private CreditDetailDao creditDetailDao;

	@Autowired
	private LoanDao loanDao;

	@Autowired
	private TCMmQueueDao queueDao;

	
	/**
	 * @description
	 * @param loanDetailDto
	 * @throws Exception
	 */
	public void updateLoanDetail(LoanDetailDto loanDetailDto) throws Exception {
		loanDao.updateLoanDetailEachStatus(SearchParamUtil.getSearchParam(loanDetailDto));

	}

	/**
	 * @description
	 * @param creditDetailDto
	 * @throws Exception
	 */
	public void updateCreditDetail(CreditDetailDto creditDetailDto) throws Exception {
		creditDetailDao.updateCreditDetailEachStatus(SearchParamUtil.getSearchParam(creditDetailDto));

	}

	/**
	 * @description
	 * @param contractNo
	 * @return
	 */
	public List<ImgurlDto> getImgurlByContractNo(final String contractNo) {
		ImgurlDto imgurlDto = new ImgurlDto();
		imgurlDto.setContractNo(contractNo);
		List<ImgurlDto> imgurlDtoList = imgurlDao.searchImgurl(SearchParamUtil.getSearchParam(imgurlDto));
		imgurlDto = null;

		return imgurlDtoList;
	}
	
	/**
	 * 	 
	 * @param imgurlDtoList
	 */
	public int insertQueue(List<ImgurlDto> imgurlDtoList) {
		int result = 0;
		TCMmQueueDTO updateQueue = null;
		for (ImgurlDto imgurlDto : imgurlDtoList) {
			updateQueue = new TCMmQueueDTO();
			updateQueue.setQueueCode(imgurlDto.getContractNo());
			List<TCMmQueueDTO> dataList = queueDao.searchTCMmQueue(SearchParamUtil.getSearchParam(updateQueue));
			if (CollectionsUtils.isNotEmpty(dataList)) {
				continue;
			}
			
			updateQueue.setQueueStatus(CimsConstant.EXE_STATUS_UNPROCESSED);
			updateQueue.setCreateTime(new Timestamp(System.currentTimeMillis()));
			queueDao.insertTCMmQueue(SearchParamUtil.getSearchParam(updateQueue));

			result += 1;
		}

		return result;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<ImgurlDto> getDealImg() {
		List<TCMmQueueDTO> queueList = getDealQueue();
		if (CollectionsUtils.isEmpty(queueList)) {
			return null;
		}

		List<TCMmQueueDTO> dealQueueList = updateDealQueue(queueList);
		if (CollectionsUtils.isEmpty(dealQueueList)) {
			return null;
		}
		
		ImgurlDto searchImgurlDto = new ImgurlDto();
		List<String> contractNoList = new ArrayList<String>();
		for (TCMmQueueDTO dealQueue : dealQueueList) {
			contractNoList.add(dealQueue.getQueueCode());
		}
		searchImgurlDto.setContractNoList(contractNoList);
		searchImgurlDto.setEachStatus(CimsConstant.LOAN_EACH_STATUS_URL);
		List<ImgurlDto> imgurlDtoList = imgurlDao.getPendingTransImgurl(SearchParamUtil.getSearchParam(searchImgurlDto));

		return imgurlDtoList;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<TCMmQueueDTO> getDealQueue() {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmQueueDTO searchQueue = new TCMmQueueDTO();
		searchQueue.setQueueStatus(CimsConstant.EXE_STATUS_UNPROCESSED);
		searchParams.put("dto", searchQueue);
		searchParams.put("start", 1);
		searchParams.put("end", DEFAULT_PAGE_SIZE);
		return queueDao.searchTCMmQueueForUpdate(searchParams);
	}

	/**
	 * 
	 * @param queueList
	 */
	public List<TCMmQueueDTO> updateDealQueue(List<TCMmQueueDTO> queueList) {
		List<TCMmQueueDTO> newList = new ArrayList<TCMmQueueDTO>();
		TCMmQueueDTO updateQueue = null;
		for (TCMmQueueDTO dealQueue : queueList) {
			updateQueue = new TCMmQueueDTO();
			updateQueue.setQueueCode(dealQueue.getQueueCode());
			updateQueue.setQueueStatus(CimsConstant.EXE_STATUS_UNPROCESSED);
			updateQueue.setNewQueueStatus(CimsConstant.EXE_STATUS_BEGIN);
			int count = queueDao.updateTCMmQueue(SearchParamUtil.getSearchParam(updateQueue));
			if (count > 0) {
				newList.add(dealQueue);
			}
		}
		
		return newList;
	}
	
}
